package service;

import mapper.BrandMapper;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;

public class BrandService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    //全部查询
    public List<Brand> selectAll() {
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //3执行查询
        List<Brand> brands = mapper.selectAll();
        //4.关闭sqlSession
        sqlSession.close();
        //5.返回数据
        return brands;
    }

    //新增品牌
    public void add(Brand brand) {
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4. 调用方法
        mapper.add(brand);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    //单个查询（根据id查询）
    public Brand selectById(int id) {
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //3执行查询
        Brand brand = mapper.selectById(id);
        //4.关闭sqlSession
        sqlSession.close();
        //5.返回数据
        return brand;
    }

    //品牌修改
    public static void update(Brand brand){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper  brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3执行修改
        brandMapper.update(brand);
        sqlSession.commit();
        //5.关闭sqlSession
        sqlSession.close();
    }

    //品牌删除
    public void delete(int id){
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4. 调用方法
        mapper.deleteById(id);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    //品牌分页查询全部
    public PageBean<Brand> selectAllPage(int currentPage, int pageSize) {
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //3执行分页查询
        PageBean<Brand> pageBean = new PageBean<>();
        //3.1查询某一页数据
        int beginIndex = (currentPage -1 ) * pageSize;//偏移量
        List<Brand> brandList = mapper.selectAllPage(beginIndex, pageSize);//所查的数据
        //3.2查询总数据条数
        int totalCount = mapper.selectCount();
        //3.3封装分页数据为PageBean
        pageBean.setData(brandList);
        pageBean.setTotalCount(totalCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        //计算总页数
        int totalPage = (int) Math.ceil(totalCount*1.0/pageSize);
        pageBean.setTotalPage(totalPage);
        //4.关闭sqlSession
        sqlSession.close();
        //5.返回数据
        return pageBean;
    }

    //根据状态查询（根据status状态查询）
    public List<Brand> selectByStatus(int status) {
        // 开启会话
        SqlSession sqlSession = factory.openSession();
        // 获取Mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //3执行查询
        List<Brand> brands = mapper.selectByStatus(status);
        //4.关闭sqlSession
        sqlSession.close();
        //5.返回数据
        return brands;
    }
}
